/**
 * Copyright (c)  HeiFeiDeFengZheng.Co.Ltd. All rights reserved.
 **/
package com.defu.newmall.service.impl;

import com.defu.newmall.common.PageResult;
import com.defu.newmall.dao.CarouselDao;
import com.defu.newmall.entity.Carousel;
import com.defu.newmall.service.NewBeeMallCarouselService;
import com.defu.newmall.utils.PageQueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 功能描述：
 *
 * @author huifeidefengzheng
 * @since 2024-03-10
 **/

@Service
public class NewBeeMallCarouselServiceImpl implements NewBeeMallCarouselService {


    @Autowired
    private CarouselDao carouselDao;

    @Override
    public PageResult getCarouselPage(PageQueryUtil pageUtil) {
        List<Carousel> carouselList = carouselDao.findCarouselList(pageUtil);
        int total = carouselDao.getTotalCarousels(pageUtil);
        PageResult<Carousel> pageResult = new PageResult<>(carouselList, total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }

    @Override
    public String saveCarousel(Carousel carousel) {
        if (carouselDao.insertSelective(carousel) > 0) {
            return "success";
        }
        return "database error";
    }

    @Override
    public String updateCarousel(Carousel carousel) {
        Carousel temp = carouselDao.selectByPrimaryKey(carousel.getCarouselId());
        if (temp == null) {
            return "database error";
        }
        temp.setCarouselRank(carousel.getCarouselRank());
        temp.setRedirectUrl(carousel.getRedirectUrl());
        temp.setCarouselUrl(carousel.getCarouselUrl());
        temp.setUpdateTime(new Date());
        if (carouselDao.updateByPrimaryKeySelective(temp) > 0) {
            return "success";
        }
        return "database error";
    }

    @Override
    public Carousel getCarouselById(Integer id) {
        return carouselDao.selectByPrimaryKey(id);
    }

    @Override
    public Boolean deleteBatch(Long[] ids) {
        if (ids.length < 1) {
            return false;
        }
        //删除数据
        return carouselDao.deleteBatch(ids) > 0;
    }
}
